package com.ruoyi.aiexperiments.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.aiexperiments.domain.ExperimentData;
import com.ruoyi.aiexperiments.service.IExperimentDataService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import java.util.Date;
import com.ruoyi.common.security.utils.SecurityUtils;

/**
 * 实验数据集Controller
 * 
 * @author czj
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/data")
public class ExperimentDataController extends BaseController
{
    @Autowired
    private IExperimentDataService experimentDataService;

    /**
     * 查询实验数据集列表
     */
    @RequiresPermissions("aiexperiments:data:list")
    @GetMapping("/list")
    public TableDataInfo list(ExperimentData experimentData)
    {
        experimentData.setDeleteFlag(0);
        startPage();
        List<ExperimentData> list = experimentDataService.selectExperimentDataList(experimentData);
        return getDataTable(list);
    }

    /**
     * 导出实验数据集列表
     */
    @RequiresPermissions("aiexperiments:data:export")
    @Log(title = "实验数据集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExperimentData experimentData)
    {
        List<ExperimentData> list = experimentDataService.selectExperimentDataList(experimentData);
        ExcelUtil<ExperimentData> util = new ExcelUtil<ExperimentData>(ExperimentData.class);
        util.exportExcel(response, list, "实验数据集数据");
    }

    /**
     * 获取实验数据集详细信息
     */
    @RequiresPermissions("aiexperiments:data:query")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") Long dataId)
    {
        return success(experimentDataService.selectExperimentDataByDataId(dataId));
    }

    /**
     * 新增实验数据集
     */
    @RequiresPermissions("aiexperiments:data:add")
    @Log(title = "实验数据集", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExperimentData experimentData)
    {
        experimentData.setCreatedBy(SecurityUtils.getUsername());
        experimentData.setCreatedAt(new Date());
        return toAjax(experimentDataService.insertExperimentData(experimentData));
    }

    /**
     * 修改实验数据集
     */
    @RequiresPermissions("aiexperiments:data:edit")
    @Log(title = "实验数据集", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExperimentData experimentData)
    {
        experimentData.setUpdatedBy(SecurityUtils.getUsername());
        experimentData.setUpdatedAt(new Date());
        return toAjax(experimentDataService.updateExperimentData(experimentData));
    }

    /**
     * 删除实验数据集
     */
    @RequiresPermissions("aiexperiments:data:remove")
    @Log(title = "实验数据集", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dataIds}")
    public AjaxResult remove(@PathVariable Long[] dataIds)
    {
        for (Long dataId : dataIds) {
            ExperimentData experimentData = experimentDataService.selectExperimentDataByDataId(dataId);
            if (experimentData == null) {
                return error("实验数据集不存在");
            }

            experimentData.setDeletedBy(SecurityUtils.getUsername());
            experimentData.setDeletedAt(new Date());
            experimentData.setDeleteFlag(1);
            experimentDataService.updateExperimentData(experimentData);
        }
        return toAjax(true);
    }
}
